package com.woniu.humanresource.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.humanresource.entity.Warehouse;
import com.woniu.humanresource.service.WarehouseService;
import com.woniu.humanresource.dao.WarehouseMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author xiang
* @description 针对表【warehouse】的数据库操作Service实现
* @createDate 2022-07-12 10:02:51
*/
@Service
public class WarehouseServiceImpl extends ServiceImpl<WarehouseMapper, Warehouse>
    implements WarehouseService{

    @Autowired
    WarehouseMapper warehouseMapper;

    //获取所有资产分类分类
    @Override
    public List<Warehouse> getAllWarehouse() {
        List<Warehouse> warehouses = warehouseMapper.selectAllWarehouse();
        return warehouses;
    }

    //添加根节点
    @Override
    public void addRootNodeWarehouse(String newWarehouseName, String newWarehouseNum, String newWarehouseAddress, String newWarehouseRemarks) {
        Warehouse newWarehouse=new Warehouse();
        newWarehouse.setName(newWarehouseName);//仓库名称
        newWarehouse.setNum(newWarehouseNum);//编号
        newWarehouse.setAddress(newWarehouseAddress);//地址
        newWarehouse.setRemarks(newWarehouseRemarks);//备注
        newWarehouse.setParentid(0);//父id

        warehouseMapper.insert(newWarehouse);
    }

    //添加子节点仓库
    @Override
    public void addWarehouse(String newWarehouseName, String newWarehouseNum, String newWarehouseAddress, String newWarehouseRemarks, Warehouse warehouse) {
        Warehouse newWarehouse=new Warehouse();
        newWarehouse.setName(newWarehouseName);//仓库名称
        newWarehouse.setNum(newWarehouseNum);//编号
        newWarehouse.setAddress(newWarehouseAddress);//地址
        newWarehouse.setRemarks(newWarehouseRemarks);//备注
        newWarehouse.setParentid(warehouse.getId());//父id

        warehouseMapper.insert(newWarehouse);
    }

    //删除勾选的节点
    @Override
    public void removeWarehousesByIds(List<Warehouse> warehouses) {
        for(Warehouse warehouse:warehouses){
            warehouseMapper.deleteById(warehouse.getId());
        }
    }

    //编辑
    @Override
    public void editWarehouse(String newWarehouseName, String newWarehouseNum, String newWarehouseAddress, String newWarehouseRemarks, Warehouse warehouse) {
        Warehouse newWarehouse=new Warehouse();
        newWarehouse.setId(warehouse.getId());//要修改的仓库的id
        newWarehouse.setName(newWarehouseName);//仓库名称
        newWarehouse.setNum(newWarehouseNum);//编号
        newWarehouse.setAddress(newWarehouseAddress);//地址
        newWarehouse.setRemarks(newWarehouseRemarks);//备注

        warehouseMapper.updateById(newWarehouse);
    }
}




